package com.xmmj.main.viewmodel

import android.Manifest
import android.view.View
import androidx.databinding.ObservableLong
import com.xmmj.baselibrary.base.activity.mvvm.BaseMvvmActivity
import com.xmmj.baselibrary.base.activity.mvvm.BaseViewModel
import com.xmmj.baselibrary.network.RxUtil.applySchedulers
import com.xmmj.baselibrary.network.RxUtil.bindToLifecycle
import com.xmmj.baselibrary.utils.SP
import com.xmmj.common.constants.IConstants
import com.xmmj.common.router.RouteNavigator.routeGuide
import com.xmmj.common.router.RouteNavigator.routeMain
import com.xmmj.main.model.MainModel
import com.xmmj.main.view.dialog.PrivacyDialog
import com.xmmj.module_main.R
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.disposables.Disposable
import java.util.concurrent.TimeUnit

class LaunchViewModel : BaseViewModel<MainModel>() {
    //权限列表
    val permissions = arrayOf(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE
    )

    //当前倒计秒数
    var time: ObservableLong = ObservableLong(3)

    //点击跳过事件
    var clickPass = View.OnClickListener {
        routeMain()
        finishActivity()
    }

    override fun onCreate() {
        super.onCreate()
        //请求权限
        requestPermission(
            permissions, {
                if (isShowPrivacy()) {
                    //显示隐私协议对话框
                    PrivacyDialog().setOnConfirmCallback {
                        //倒计时
                        countdown()
                        //设置不再显示隐私协议对话框
                        setShowPrivacy(false)
                    }.show(lifecycleOwner)
                } else {
                    countdown()
                }
            }, {
                countdown()
            },
            applicationContext.getString(R.string.permissions_description)
        )
    }

    /**
     * 是否显示隐私协议
     */
    private fun isShowPrivacy(): Boolean {
        return SP(applicationContext).getBoolean(IConstants.SHOW_PRIVACY, true)
    }

    /**
     * 设置是否显示隐私协议
     */
    private fun setShowPrivacy(show: Boolean) {
        SP(applicationContext).putBoolean(IConstants.SHOW_PRIVACY, show)
    }

    /**
     * 是否显示引导页
     */
    private fun isShowGuide(): Boolean {
        return SP(applicationContext).getBoolean(IConstants.SHOW_GUIDE, true)
    }

    /**
     * 倒计时任务
     */
    fun countdown() {
        Observable.interval(0, 1, TimeUnit.SECONDS)
            .compose(applySchedulers())
            .compose(bindToLifecycle(lifecycleOwner))
            .take(3)
            .map { 3.minus(it) }
            .subscribe(object : Observer<Long> {
                override fun onSubscribe(d: Disposable) {

                }

                override fun onNext(t: Long) {
                    time.set(t)
                }

                override fun onError(e: Throwable) {

                }

                override fun onComplete() {
                    finish()
                }
            })
    }

    /**
     * 倒计时结束要执行的逻辑
     */
    fun finish() {
        if (isShowGuide()) {
            routeGuide()
        } else {
            routeMain()
        }
        finishActivity()
    }
}